/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.uff.ihc.ontologico.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 *
 * @author luiz.silva
 */
public class FileUtil {
    
     public static String lerArquivoTexto(File file) {
        
        try {
            StringBuilder fileData = new StringBuilder(1000);
            FileReader fileReader = new FileReader(file);
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "ISO-8859-1");
            BufferedReader reader = new BufferedReader(isr);
            char[] buf = new char[1024];
            int numRead=0;
            while((numRead=reader.read(buf)) != -1){
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
            fileReader.close();
            
            return fileData.toString();
            
        } catch (IOException e) {
            System.err.printf("Erro na abertura do arquivo: %s.\n",
                    e.getMessage());
        }
        
        return "";
    }
     
    public static void escreverArquivo(File file, String xml) {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "ISO-8859-1");
            xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>" + xml;
            osw.write(xml);
            osw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
